package com.skzh.flightplans.service.impl;

import java.time.LocalDate;
import java.util.List;

import com.skzh.common.annotation.DataSource;
import com.skzh.common.enums.DataSourceType;
import com.skzh.common.utils.DateUtils;
import com.skzh.flightplans.domain.Waypoints;
import com.skzh.flightplans.mapper.WaypointsMapper;
import com.skzh.flightplans.service.IWaypointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.skzh.flightplans.mapper.FlightPlansMapper;
import com.skzh.flightplans.domain.FlightPlans;
import com.skzh.flightplans.service.IFlightPlansService;

/**
 * 航线列表Service业务层处理
 *
 * @author skzh
 * @date 2025-03-31
 */
@Service
public
class FlightPlansServiceImpl implements IFlightPlansService {
    @Autowired
    private FlightPlansMapper flightPlansMapper;
    @Autowired
    private WaypointsMapper waypointsMapper;
    @Autowired
    private IWaypointsService waypointsService;

    /**
     * 查询航线列表
     *
     * @param fpId 航线列表主键
     * @return 航线列表
     */
    @Override
    public FlightPlans selectFlightPlansByFpId(String fpId) {
        return flightPlansMapper.selectFlightPlansByFpId(fpId);
    }

    /**
     * 查询航线列表列表
     *
     * @param flightPlans 航线列表
     * @return 航线列表
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public List<FlightPlans> selectFlightPlansList(FlightPlans flightPlans) {
        return flightPlansMapper.selectFlightPlansList(flightPlans);
    }

    /**
     * 新增航线列表
     *
     * @param flightPlans 航线列表
     * @return 结果
     */
    @Override
    public int insertFlightPlans(FlightPlans flightPlans) {
        flightPlans.setCreateTime(DateUtils.toDate(LocalDate.now()));
        return flightPlansMapper.insertFlightPlans(flightPlans);
    }

    /**
     * 修改航线列表
     *
     * @param flightPlans 航线列表
     * @return 结果
     */
    @Override
    public int updateFlightPlans(FlightPlans flightPlans) {
        flightPlans.setUpdateTime(DateUtils.getNowDate());
        return flightPlansMapper.updateFlightPlans(flightPlans);
    }

    /**
     * 批量删除航线列表
     *
     * @param fpIds 需要删除的航线列表主键
     * @return 结果
     */
    @Override
    public int deleteFlightPlansByFpIds(String[] fpIds) {
        // 先物理删除关联的航点
        waypointsMapper.deleteWaypointsByFpIds(fpIds);

        return flightPlansMapper.deleteFlightPlansByFpIds(fpIds);
    }

    /**
     * 删除航线列表信息
     *
     * @param fpId 航线列表主键
     * @return 结果
     */
    @Override
    public int deleteFlightPlansByFpId(String fpId) {
        return flightPlansMapper.deleteFlightPlansByFpId(fpId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务管理，使用spirng的@Transactional注解，底层实现是基于数据库事务的
    public int insertFlightPlansAndWaypoints(FlightPlans newflightPlans, List<Waypoints> waypoints) {
        newflightPlans.setCreateTime(DateUtils.getNowDate());
        int rows = flightPlansMapper.insertFlightPlans(newflightPlans);

        // 获取航线id
        String fpId = newflightPlans.getFpId();

        // 模拟延迟以体现异步作用
        try {
            Thread.sleep(9000); // 延迟9秒
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // 恢复中断状态
            throw new RuntimeException("Thread was interrupted", e);
        }

        // 批量设置 wpFlightPlanId 和 wpIndex
        for (int i = 0; i < waypoints.size(); i++) {
            Waypoints waypoint = waypoints.get(i);
            waypoint.setWpFlightPlanId(fpId);
            waypoint.setWpIndex((long) i);
        }

        // 模拟添加一个错误，测试事务回滚
        if (waypoints != null && !waypoints.isEmpty()) {
            throw new RuntimeException("测试事务回滚：批量插入航点数据时发生错误");
        }

        // 批量插入航点数据
        waypointsMapper.batchInsertWaypoints(waypoints);

        return rows;
    }
}
